<?php

require 'Zend/Loader.php';

$root = dirname(__FILE__).'/..';

/**
 * Autoloads all class when they are instantiated (lazy loading)
 *
 * @param string $class the name of the class to load
 */
function __autoload($class)
{
    return @Zend_Loader::autoload($class);
}

function log_err($msg)
{
    Zend_Registry::get('logger')->log($msg,Zend_Log::ERR);
}

function log_dbg($msg)
{
    Zend_Registry::get('logger')->log($msg,Zend_Log::DEBUG);
}


function cfg()
{
    $args = func_get_args();
    $key = array_shift($args);
    $keyName = str_replace('.','->',$key);
    $value = null;
    eval('$value = @Zend_Registry::get("config")->'.$keyName.';');
    for($i=0;$i<count($args);$i++)
    {
        $value = str_replace('%{'.($i+1).'}',$args[$i],$value);
    }
    return $value;
}

function get($key)
{
	try{
		$value=Zend_Registry::get($key);
	}catch(Zend_Exception $e){
	    $value = null;
	}
	
	if(Zend_Registry::isRegistered($key)===true)
	{
	    return $value;
	}
	if($value===NULL)
	{
	    $value=cfg($key);
    }
    return $value;
}

function set($key,$value)
{
    Zend_Registry::set($key,$value);
}


set_include_path(
    // controllers directories
    $root."/application/default/controllers". PATH_SEPARATOR .
    // models directories
    $root."/application/default/models"     . PATH_SEPARATOR .
    // services directories
    $root."/application/default/services"   . PATH_SEPARATOR .
    // exceptions directories
    $root."/application/default/exceptions" . PATH_SEPARATOR .
    // tests directories
    $root."/application/default/tests"      . PATH_SEPARATOR .
    // existing include path
    get_include_path()
);

$config = new Zend_Config_Ini(dirname(__FILE__).'/../conf/web.ini',$LIGHTFEED_ENV);
Zend_Registry::set('config',$config);

$logger = new Zend_Log();
$logger->setEventItem('pid', getmypid());
$loggers = explode(',',$config->loggers);
foreach($loggers as $loggername){
    if($loggername==='file'){
        $logger->addWriter(new Zend_Log_Writer_Stream($config->logger->$loggername->file));
        eval('$value = Zend_Log::'.strtoupper($config->logger->$loggername->priority).';');
        $logger->addFilter(new Zend_Log_Filter_Priority($value));
    }
}
Zend_Registry::set('logger',$logger);

// }}}
// {{{ loads database

if(@$config->database!==null){
    Zend_Db_Table_Abstract::setDefaultAdapter(Zend_Db::factory($config->database));
}
